const XLSX = require('xlsx');
const zh = require('./zh-cn');
const en = require('./en');

// 用于存储扁平化后的键值对
const flattenedPairs = [];

// 递归函数来扁平化嵌套对象
function flattenObject(zhObj, enObj, prefix = '') {
  for (const key in zhObj) {
      const zhVal = zhObj[key];
      const enVal = enObj? enObj[key]: '';
      const newKey = prefix ? `${prefix}.${key}` : key;
      
      if (typeof zhVal === 'object' && zhVal !== null) {
          flattenObject(zhVal, enObj[key], newKey);
      } else {
          flattenedPairs.push(['message.',newKey, zhVal, enVal]);
      }
  }
}

// 扁平化message对象
flattenObject(zh.message, en.message);

// 创建工作簿和工作表
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([
    ['key前置','key后续', '中文', '英文'], // 表头
    ...flattenedPairs
]);

// 将工作表添加到工作簿
XLSX.utils.book_append_sheet(wb, ws, 'Translation');

// 保存Excel文件
XLSX.writeFile(wb, 'translation.xlsx');

console.log('Excel文件已生成！');

